package com.hyphenate.easeui.model;

import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;

public class recordBean_hy {
	public final static String TABLE_NAME="hyphenate";

	public final static String ID="_id";
	public final static String MEMBERID="memberid";//用户id
	public final static String IMUSERID="imuserid";//环信id
	public final static String NICKNAME="nickname";//昵称
	public final static String FACEIMGID="faceimgid";//头像id
	public final static String FACEIMG="faceimg";//头像url
	public final static String OTHER="other";//其他
	public final static String ISME="isme";//是不是我本人 1为是 0为不是 2为店东
	private static recordBean_hy instance;
	public final static recordBean_hy getInstance(){
		if(null==instance){
			instance=new recordBean_hy();
		}return instance;
	}
  public final  static String sqlcreateTable(){
	  StringBuffer buffer =new StringBuffer();
	  buffer.append("create table if not exists ").append(TABLE_NAME).append("(");
	  buffer.append(ID).append(" INTEGER  primary key AUTOINCREMENT,");
	  buffer.append(MEMBERID).append(" text,");
	  buffer.append(IMUSERID).append(" text,");
	  buffer.append(NICKNAME).append(" text,");
	  buffer.append(FACEIMGID).append(" text,");
	  buffer.append(FACEIMG).append(" text,");
	  buffer.append(ISME).append(" text,");
	  buffer.append(OTHER).append(" text")
	  .append(")");
	return buffer .toString();

  }
  public final static String sqldropTable(){
	  StringBuffer buffer=new StringBuffer();
	  buffer.append("create table if sxists ").append(TABLE_NAME);
	  return buffer.toString();
  }
	public final boolean saveMember(final DBHelper_hy dbHelper,String menberid,String imuserid,String nickname,
									String faceimgid,String faceimg,String isme){
		if(TextUtils.isEmpty(menberid)){
			menberid="";
		}
		if(TextUtils.isEmpty(imuserid)){
			imuserid="";
		}
		if(TextUtils.isEmpty(nickname)){
			nickname="";
		}
		if(TextUtils.isEmpty(faceimgid)){
			faceimgid="";
		}
		if(TextUtils.isEmpty(faceimg)){
			faceimg="";
		}
		if(TextUtils.isEmpty(isme)){
			isme="";
		}
		dbHelper.open();
		ContentValues contentsValues=null;
		Cursor cursor=dbHelper.findList(TABLE_NAME, new String[]{
				ID, IMUSERID,FACEIMG,NICKNAME},  IMUSERID+"="+"\""+imuserid+"\"",null,null, null,  ID+" asc");
		String cur_imuserid="";
		String cur_faceimg="";
		String cur_nickname="";
		while(cursor.moveToNext()){
			int id= cursor.getInt(cursor.getColumnIndex(ID));
			cur_imuserid=cursor.getString(cursor.getColumnIndex(IMUSERID));
			cur_faceimg=cursor.getString(cursor.getColumnIndex(FACEIMG));
			cur_nickname=cursor.getString(cursor.getColumnIndex(NICKNAME));
			break;
		}
		cursor.close();
		if(TextUtils.isEmpty(cur_imuserid)){
			contentsValues=new ContentValues();
			contentsValues.put(MEMBERID,menberid);
			contentsValues.put(IMUSERID, imuserid);
			contentsValues.put(NICKNAME, nickname);
			contentsValues.put(FACEIMGID, faceimgid);
			contentsValues.put(FACEIMG, faceimg);
			contentsValues.put(ISME, isme);

			dbHelper.insert(TABLE_NAME, contentsValues);

		}else {
		//if(!cur_faceimg.equals(faceimg)||!cur_nickname.equals(nickname)){


			contentsValues=new ContentValues();
			contentsValues.put(MEMBERID,menberid);
			contentsValues.put(NICKNAME, nickname);
			contentsValues.put(FACEIMGID, faceimgid);
			contentsValues.put(FACEIMG, faceimg);
			contentsValues.put(ISME, isme);
			dbHelper.update(TABLE_NAME, contentsValues, IMUSERID + "="+"\""+imuserid+"\"",
					null);


		}
		dbHelper.closeclose();
		return true;
	}
	//查找我的用户信息
	public final HyUserDao  findMember_Me(final DBHelper_hy dbHelper){
		dbHelper.open();

		Cursor cursor=dbHelper.findList(TABLE_NAME, new String[]{
				ID, MEMBERID,IMUSERID,NICKNAME,FACEIMGID,FACEIMG,ISME},  ISME+"="+"\""+"1"+"\"",null,null, null,  null);
		HyUserDao dbHelper_hy=null;
		while(cursor.moveToNext()){
			int id=cursor.getInt(cursor.getColumnIndex(ID));
			String  memberid=cursor.getString(cursor.getColumnIndex(MEMBERID));
			String  imuserid=cursor.getString(cursor.getColumnIndex(IMUSERID));
			String  nickname=cursor.getString(cursor.getColumnIndex(NICKNAME));
			String  faceimgid=cursor.getString(cursor.getColumnIndex(FACEIMGID));
			String  faceimg=cursor.getString(cursor.getColumnIndex(FACEIMG));
			String  isme=cursor.getString(cursor.getColumnIndex(ISME));
			dbHelper_hy=new HyUserDao(id,memberid,imuserid,nickname,faceimgid,faceimg,isme,"");
			break;
		}

		cursor.close();
		dbHelper.closeclose();
		return dbHelper_hy;
	}
	//查找他人的用户信息
	public final HyUserDao  findMember_other(final DBHelper_hy dbHelper,String imuserid){
		dbHelper.open();

		Cursor cursor=dbHelper.findList(TABLE_NAME, new String[]{
				ID, MEMBERID,IMUSERID,NICKNAME,FACEIMGID,FACEIMG,ISME},  IMUSERID+"="+"\""+imuserid+"\"",null,null, null,  null);
		HyUserDao dbHelper_hy=null;
		while(cursor.moveToNext()){
			int id=cursor.getInt(cursor.getColumnIndex(ID));
			String  memberid=cursor.getString(cursor.getColumnIndex(MEMBERID));
			String  nickname=cursor.getString(cursor.getColumnIndex(NICKNAME));
			String  faceimgid=cursor.getString(cursor.getColumnIndex(FACEIMGID));
			String  faceimg=cursor.getString(cursor.getColumnIndex(FACEIMG));
			String  isme=cursor.getString(cursor.getColumnIndex(ISME));
			dbHelper_hy=new HyUserDao(id,memberid,imuserid,nickname,faceimgid,faceimg,isme,"");
			break;
		}

		cursor.close();
		dbHelper.closeclose();
		return dbHelper_hy;
	}
//	//查找areacode
//	public final String  findAREA_CODE_SHORT(final DBHelper_hy dbHelper,String txt_address){
//		dbHelper.open();
//		txt_address=txt_address.substring(0,2);
//		Cursor cursor=dbHelper.findList(TABLE_NAME, new String[]{
//				AREA_NAME, AREA_CODE}, AREA_NAME+" like "+"'%"+txt_address+"%'",null,null, null,  null);
//		Log.i("mylog",AREA_NAME+" like "+"'%"+txt_address+"%'");
//		String s_area_code="";
//		while(cursor.moveToNext()){
//
//			s_area_code=cursor.getString(cursor.getColumnIndex(AREA_CODE));
//		}
//
//		cursor.close();
//		dbHelper.closeclose();
//		return s_area_code;
//	}
//	//查找地区全名
//	public final String  findAREA_PATH(final DBHelper_hy dbHelper,String area_code){
//		dbHelper.open();
//		Cursor cursor=dbHelper.findList(TABLE_NAME, new String[]{
//				PATH, AREA_CODE},  AREA_CODE+"="+"\""+area_code+"\"",null,null, null,  null);
//		String s_area_path="";
//		while(cursor.moveToNext()){
//
//			s_area_path=cursor.getString(cursor.getColumnIndex(PATH));
//		}
//
//		cursor.close();
//		dbHelper.closeclose();
//		return s_area_path;
//	}


//  
//  public final boolean saveDiaryRecord(final DBHelper dbHelper,String date,String text,String path){
//	  dbHelper.open();
//	  ContentValues contentsValues=null;
//       DiaryDao diadao=null;
//	  Cursor cursor=dbHelper.findList(TABLE_NAME, new String[]{
//	  ID, DATE,ISDIARY},  DATE+"="+"\""+date+"\"",null,null, null,  ID+" asc");
//       while(cursor.moveToNext()){
//          int id= cursor.getInt(cursor.getColumnIndex(ID));
//          String s_date=cursor.getString(cursor.getColumnIndex(DATE));
//          int isdia=cursor.getInt(cursor.getColumnIndex(ISDIARY));
//          diadao=new DiaryDao(id,isdia, s_date,"","");
//          break;
//      }
//      cursor.close();
//      if(diadao!=null){
//    	  contentsValues=new ContentValues();
//    	//  contentsValues.put(ID, null);
//    	  contentsValues.put(ISDIARY, 1);
//    	  contentsValues.put(DIATEXT, text);
//    	  contentsValues.put(DIAPATH, path);
// 		 dbHelper.update(TABLE_NAME, contentsValues,DATE+"="+"\""+date+"\"",
// 				 null);  
// 		
//      }else{
//    	  contentsValues=new ContentValues();
//    	  contentsValues.put(ISDIARY, 1);
//    	  contentsValues.put(DIATEXT, text);
//    	  contentsValues.put(DATE, date);
//    	  String d=date.replace("-", "");
//    	  contentsValues.put(INTDATE, Integer.parseInt(d));
//    	  contentsValues.put(DIAPATH, path);
//    	  dbHelper.insert(TABLE_NAME,contentsValues);
//    		
//     }
//      dbHelper.closeclose();
//		return true;
//  }
//  public final void removeAll(final DBHelper dbHelper){
//	  dbHelper.open();
//	  String  sqlDel="delete from "+TABLE_NAME;
//	  dbHelper.execSQL(sqlDel);
//	  dbHelper.closeclose();
//  }
//  public final boolean removeOneDate(final DBHelper dbHelper,String date){
//	  dbHelper.open();
//	  int ishw=0;
//	  Cursor cursor=dbHelper.findList(TABLE_NAME, new String[]{
//			  ID, DATE,ISHW},  DATE+"="+"\""+date+"\"",null,null, null,  ID+" asc");
//		       while(cursor.moveToNext()){
//		    	   ishw=cursor.getInt(cursor.getColumnIndex(ISHW));
//		          break;
//		      }
//	   cursor.close();
//	   if(ishw==1){
//		   ContentValues  contentsValues=new ContentValues();
//	    	//  contentsValues.put(ID, null);
//	    	  contentsValues.put(ISDIARY, 0);
//	    	  contentsValues.put(DIATEXT, "");
//	    	  contentsValues.put(DIAPATH, "");
//	 		 dbHelper.update(TABLE_NAME, contentsValues,DATE+"="+"\""+date+"\"",
//	 				 null);  
//	   }else{
//	  boolean result=dbHelper.delete(TABLE_NAME, DATE+"="+"\""+date+"\"", null);}
//      dbHelper.closeclose();
//      return true;
//  }

}
